package com.example.administrator.college02.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.administrator.college02.BaseFragment;
import com.example.administrator.college02.R;
import com.example.administrator.college02.adapter.MyFragmentPagerAdapter;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/7/11.
 */
public class FriendFragment extends BaseFragment implements ViewPager.OnPageChangeListener{
    @BindView(R.id.friend_radio_group) RadioGroup rgFriend;
    @BindView(R.id.friend_tab)
    RadioButton friendTab;
    @BindView(R.id.friend_group_tab)
    RadioButton friendGroupTab;
    ArrayList<Fragment> mViews;
    @BindView(R.id.friend_viewPager)
    ViewPager mViewPager;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_friend_layout,container,false);
        unbinder = ButterKnife.bind(this,view);
        initViews();
        mViewPager.addOnPageChangeListener(this);
        rgFriend.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.friend_tab:
                        mViewPager.setCurrentItem(0);
                        break;
                    case R.id.friend_group_tab:
                        mViewPager.setCurrentItem(1);
                        break;
                }
            }
        });
        return view;
    }
    public void initViews() {
        mViews = new ArrayList<>();
        FragmentFriendChild fragmentFriendChild = new FragmentFriendChild();
        FragmentGroupChild fragmentGroupChild = new FragmentGroupChild();
        mViews.add(fragmentFriendChild);
        mViews.add(fragmentGroupChild);
        mViewPager.setAdapter(new MyFragmentPagerAdapter(getChildFragmentManager(), mViews));

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {
//          state状态 0 什么都没做 1 正在滑动 2滑动完毕
        if (state == 2){
            switch (mViewPager.getCurrentItem()){
                case 0:
                    friendTab.setChecked(true);
                    break;
                case 1:
                    friendGroupTab.setChecked(true);
                    break;
            }
        }
    }
}
